package com.xyy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xyy.common.PageResult;
import com.xyy.common.Result;
import com.xyy.entity.DoctorActivityRegistration;
import com.xyy.entity.DoctorActivityRegistrationVO;
import com.xyy.service.DoctorActivityRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员医生报名管理控制器
 */
@RestController
@RequestMapping("/admin/doctor-registration")
@CrossOrigin
public class AdminDoctorRegistrationController {
    
    @Autowired
    private DoctorActivityRegistrationService doctorActivityRegistrationService;
    
    /**
     * 获取活动的所有报名医生列表（包含医生详细信息）
     */
    @GetMapping("/activity/{activityId}")
    public Result<PageResult<DoctorActivityRegistrationVO>> getRegistrationsByActivity(
            @PathVariable Long activityId,
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size) {
        
        IPage<DoctorActivityRegistrationVO> page = doctorActivityRegistrationService
            .getRegistrationsByActivityWithDoctorInfo(activityId, current, size);
        
        PageResult<DoctorActivityRegistrationVO> pageResult = new PageResult<>();
        pageResult.setRecords(page.getRecords());
        pageResult.setTotal(page.getTotal());
        pageResult.setCurrent(page.getCurrent());
        pageResult.setSize(page.getSize());
        
        return Result.success(pageResult);
    }
    
    /**
     * 管理员审核医生报名并给予积分
     */
    @PostMapping("/approve/{registrationId}")
    public Result<String> approveRegistrationAndGivePoints(
            @PathVariable Long registrationId,
            @RequestParam String status,
            @RequestParam(required = false) Integer pointsEarned) {
        
        // 验证状态
        if (!"COMPLETED".equals(status)) {
            return Result.error("无效的审核状态，只能设置为已完成");
        }
        
        // 验证积分（如果提供的话）
        if (pointsEarned != null && pointsEarned < 0) {
            return Result.error("积分数量不能为负数");
        }
        
        boolean success = doctorActivityRegistrationService
            .approveRegistrationAndGivePoints(registrationId, status, pointsEarned);
        
        if (success) {
            return Result.success("审核成功，积分已发放");
        } else {
            return Result.error("审核失败");
        }
    }
    
    /**
     * 批量审核医生报名并给予积分
     */
    @PostMapping("/approve/batch")
    public Result<String> batchApproveRegistrations(
            @RequestBody BatchApprovalRequest request) {
        
        int successCount = 0;
        int totalCount = request.getRegistrationIds().size();
        
        for (Long registrationId : request.getRegistrationIds()) {
            boolean success = doctorActivityRegistrationService
                .approveRegistrationAndGivePoints(registrationId, request.getStatus(), request.getPointsEarned());
            if (success) {
                successCount++;
            }
        }
        
        return Result.success(String.format("批量审核完成，成功 %d/%d 条", successCount, totalCount));
    }
    
    /**
     * 批量审核请求DTO
     */
    public static class BatchApprovalRequest {
        private java.util.List<Long> registrationIds;
        private String status;
        private Integer pointsEarned; // 可选，如果不提供则使用活动默认积分
        
        // getters and setters
        public java.util.List<Long> getRegistrationIds() {
            return registrationIds;
        }
        
        public void setRegistrationIds(java.util.List<Long> registrationIds) {
            this.registrationIds = registrationIds;
        }
        
        public String getStatus() {
            return status;
        }
        
        public void setStatus(String status) {
            this.status = status;
        }
        
        public Integer getPointsEarned() {
            return pointsEarned;
        }
        
        public void setPointsEarned(Integer pointsEarned) {
            this.pointsEarned = pointsEarned;
        }
    }
}
